//
//  ByWebViewController.m
//  beibei_ios
//
//  Created by mc on 16/5/31.
//  Copyright © 2016年 hebidu. All rights reserved.
//

#import "ByWebViewController.h"

@interface ByWebViewController ()<UIScrollViewDelegate>
{
    UILabel *_loadLabel; // 网页加载提示文字
    UIActivityIndicatorView *_indicatorView; // 网页加载菊花转动
    UILabel *_notesLable;//提供者
}
@end

@implementation ByWebViewController

// 自定义初始化方法，获取所要显示的网页的网址，并显示该网页
- (instancetype)initWithUrlString:(NSString *)urlString {
    self = [super init];
    if (self) {
    
        // 创建网页webView
        _webView = [[UIWebView alloc] init];
        _webView.delegate = self;
        _webView.scalesPageToFit = YES;
        _webView.scrollView.delegate = self;
        [self.view addSubview:_webView];
        [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view).insets(UIEdgeInsetsZero).priorityMedium();
            if (@available(iOS 11.0, *)) {
                make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
            }
        }];
        
        UIView *backgroundView = [[UIView alloc] init];
        backgroundView.backgroundColor = rgba(46, 49, 50, 1);
        [_webView insertSubview:backgroundView atIndex:0];
        [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(_webView).insets(UIEdgeInsetsZero).priorityMedium();
        }];
        
        _notesLable = [[UILabel alloc] init];
        NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
        NSString *name = [NSString stringWithFormat:@"网页由 %@ 提供",appName];
        _notesLable.text = name;
        _notesLable.textAlignment = NSTextAlignmentCenter;
        _notesLable.textColor = [UIColor grayColor];
        _notesLable.backgroundColor = [UIColor clearColor];
        _notesLable.font = FONT(12);
        _notesLable.hidden = YES;
        [backgroundView addSubview:_notesLable];
        [_webView insertSubview:backgroundView atIndex:0];
        [_notesLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(_webView);
            make.height.mas_equalTo(20);
            make.left.equalTo(_webView);
            make.top.equalTo(_webView).offset(10);
        }];
       
        
        // 发送请求，获取该网页内容
        urlString = [[NSString stringWithFormat:@"%@%@",BOYE_WEB_BASE_URL,urlString] stringByEncodingURLFormat];
        NSLog(@"urlString:%@", urlString);
        NSURL *url = [NSURL URLWithString:urlString];
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
        [_webView loadRequest:request];
    }
    return self;
}

- (instancetype)initWithTotalUrlString:(NSString *)urlString {
    self = [super init];
    if (self) {
        
        // 创建网页webView
        _webView = [[UIWebView alloc] init];
        _webView.delegate = self;
        _webView.scalesPageToFit = YES;
        _webView.scrollView.delegate = self;
        [self.view addSubview:_webView];
        [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view).insets(UIEdgeInsetsZero).priorityMedium();
            if (@available(iOS 11.0, *)) {
                make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
            }
        }];
        
        UIView *backgroundView = [[UIView alloc] init];
        backgroundView.backgroundColor = rgba(46, 49, 50, 1);
        [_webView insertSubview:backgroundView atIndex:0];
        [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(_webView).insets(UIEdgeInsetsZero).priorityMedium();
        }];
        
        _notesLable = [[UILabel alloc] init];
        NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
        NSString *name = [NSString stringWithFormat:@"网页由 %@ 提供",appName];
        _notesLable.text = name;
        _notesLable.textAlignment = NSTextAlignmentCenter;
        _notesLable.textColor = [UIColor grayColor];
        _notesLable.backgroundColor = [UIColor clearColor];
        _notesLable.font = FONT(12);
        _notesLable.hidden = YES;
        [backgroundView addSubview:_notesLable];
        [_webView insertSubview:backgroundView atIndex:0];
        
        [_notesLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(_webView);
            make.height.mas_equalTo(20);
            make.left.equalTo(_webView);
            make.top.equalTo(_webView).offset(10);
        }];
        
        // 发送请求，获取该网页内容
        urlString = [[NSString stringWithFormat:@"%@",urlString] stringByEncodingURLFormat];
        NSLog(@"urlString:%@", urlString);
        NSURL *url = [NSURL URLWithString:urlString];
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
        [_webView loadRequest:request];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addLeftBackBarButtonItem];
    
    _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    _indicatorView.hidden = YES;
}
- (void)setTitleString:(NSString *)titleString {
    
    _titleString = titleString;
    self.title = _titleString;
}



#pragma mark - UIVebView代理方法
// 开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView {
    
//    // 加载网页时，菊花转动
//    _indicatorView.color = [UIColor lightGrayColor];
//    CGPoint point = CGPointMake(_webView.width / 2, _webView.height / 2);
//    _indicatorView.center = point;
//    [_webView addSubview:_indicatorView];
//    _indicatorView.hidesWhenStopped = YES;
//    _indicatorView.hidden = NO;
//    [_indicatorView startAnimating];
//    // 10秒后还未加载完就隐藏菊花
//    [self performSelector:@selector(delayAction) withObject:nil afterDelay:5];

}

//- (void)delayAction {
//
//    [_indicatorView stopAnimating];
//    [_indicatorView removeFromSuperview];
//    _indicatorView = nil;
//}

// 加载结束
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
//    // 加载提示移除
//    [_indicatorView stopAnimating];
//    [_indicatorView removeFromSuperview];
//    _indicatorView = nil;
}


#pragma mark - UIScrollViewDelegate代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat yOffset = scrollView.contentOffset.y;
    //向上偏移量变正  向下偏移量变负
    if (yOffset < 0) {
        _notesLable.hidden = NO;
    }else{
        _notesLable.hidden = YES;
    }
    
}


#pragma mark - 返回上一界面
- (void)leftItemsAction:(NSInteger)leftIndex {
    if (_webView.canGoBack) {
        [_webView goBack];
    }
    else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}





@end
